/*
ID: pallab81
PROG:
LANG: C++
*/

//"I have not failed, I have just found 10000 ways that won't work.

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i1,st,ed) for(int i1 = st,j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) foR( i , st , ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip( X ) sleep( X )
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define line puts("")

template<class T1>
inline void debug(T1 _x) {
    cout<<_x<<'\n';
}
template<class T1, class T2>
inline void debug(T1 _x,T2 _y) {
    cout<<_x<<' '<<_y<<'\n';
}
template<class T1, class T2, class T3>
inline void debug(T1 _x,T2 _y,T3 _z) {
    cout<<_x<<' '<<_y<<' '<<_z<<'\n';
}
template<class T1, class T2, class T3, class T4>
inline void debug(T1 _x,T2 _y,T3 _z,T4 _zz) {
    cout<<_x<<' '<<_y<<' '<<_z<<' '<<_zz<<'\n';
}
template< class T1>
inline void debug(T1 _array,int _size) {
    cout<<"[";
    for (int i=0;i<_size;++i) {
        cout<<' '<<_array[i];
    }
    puts(" ]");
}

class NinePuzzle {
public:
	int getMinimumCost(string init, string goal);
};
string moves[]={ "12","0234","0145","1476","312587","4298","37","6348","7459","89" };

int NinePuzzle :: getMinimumCost(string init, string goal) {

    map<string,int> mp;
    queue< string > q;
    q.push(init);
    mp[init]=0;

    while(!q.empty()){
        string u = q.front();
        q.pop();

        fo(i,10){
            if( u[i]=='*' )continue;
            string move=moves[i];
            fo(j,LN(move)){
                int indx=move[j]-'0';
                if( u[indx]=='*' ){
                    string v=u;
                    swap(v[i],v[indx]);
                    if(mp.count(v)<1){
                        mp[v]=1;
                        q.push(v);
                    }
                }
            }
        }
    }
    int best=10000;
    foit(it,mp){
        string cur=it->first;
        int d=0;
        fo(i,LN(cur)){
            if(cur[i]=='*'||goal[i]=='*'){
                   if(cur[i]!=goal[i]){d+=10000;break;}
            }
            else{
                d+= (cur[i]!=goal[i]);
            }
        }
        best=min(best,d);

    }
    //string str;cin>>str;
return best;
}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, string p0, string p1, bool hasAnswer, int p2) {
	cout << "Test " << testNum << ": [" << "\"" << p0 << "\"" << "," << "\"" << p1 << "\"";
	cout << "]" << endl;
	NinePuzzle *obj;
	int answer;
	obj = new NinePuzzle();
	clock_t startTime = clock();
	answer = obj->getMinimumCost(p0, p1);
	clock_t endTime = clock();
	delete obj;
	bool res;
	res = true;
	cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
	if (hasAnswer) {
		cout << "Desired answer:" << endl;
		cout << "\t" << p2 << endl;
	}
	cout << "Your answer:" << endl;
	cout << "\t" << answer << endl;
	if (hasAnswer) {
		res = answer == p2;
	}
	if (!res) {
		cout << "DOESN'T MATCH!!!!" << endl;
	} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
		cout << "FAIL the timeout" << endl;
		res = false;
	} else if (hasAnswer) {
		cout << "Match :-)" << endl;
	} else {
		cout << "OK, but is it right?" << endl;
	}
	cout << "" << endl;
	return res;
}
int main() {
	bool all_right;
	all_right = true;

	string p0;
	string p1;
	int p2;

	{
	// ----- test 0 -----
	p0 = "BG*YRGRRYR";
	p1 = "BGGY*YRRRR";
	p2 = 0;
	all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
	// ------------------
	}

	{
	// ----- test 1 -----
	p0 = "GBBB*BGBBG";
	p1 = "RYYY*YRYYR";
	p2 = 9;
	all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
	// ------------------
	}

	{
	// ----- test 2 -----
	p0 = "RRBR*BRBBB";
	p1 = "BBRB*RBRRR";
	p2 = 1;
	all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
	// ------------------
	}

	if (all_right) {
		cout << "You're a stud (at least on the example cases)!" << endl;
	} else {
		cout << "Some of the test cases had errors." << endl;
	}
	return 0;
}
// END KAWIGIEDIT TESTING


//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
